package com.github.binarywang.demo.wechat.utils;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import org.springframework.web.multipart.MultipartFile;

public class FileUtils {
	// private static String BASE_PATH = "/opt/wechat";
	private static String BASE_PATH = "/Users/sangshuaijie/Documents/pic";

	private static String IMAGE_BASE_PATH = BASE_PATH + "/image";
	private static String VIDEO_BASE_PATH = BASE_PATH + "/video";

	public static String writeImageToLocal(File file) throws Exception {
		OutputStream os = null;
		InputStream inputStream = new FileInputStream(file);

		try {
			byte[] bs = new byte[1024];

			File tempFile = new File(IMAGE_BASE_PATH);
			if (!tempFile.exists()) {
				tempFile.mkdirs();
			}
			os = new FileOutputStream(IMAGE_BASE_PATH + File.separator + file.getName());
			int len;
			while ((len = inputStream.read(bs)) != -1) {
				os.write(bs, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		try {
			os.close();
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return IMAGE_BASE_PATH + File.separator + file.getName();
	}

	public static String writeVideoToLocal(MultipartFile file) throws Exception {
		String fileName = file.getOriginalFilename();

		String suffixName = fileName.substring(fileName.lastIndexOf("."));

		fileName = UUID.randomUUID() + suffixName;
		File dest = new File(VIDEO_BASE_PATH + File.separator + fileName);

		if (!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}
		file.transferTo(dest);
		return VIDEO_BASE_PATH + File.separator + fileName;
	}

	public static String writeFileToLocal(MultipartFile file) throws Exception {
		String fileType = file.getContentType();
		String fileName = file.getOriginalFilename();

		String suffixName = fileName.substring(fileName.lastIndexOf("."));

		fileName = UUID.randomUUID() + suffixName;
		File dest = null;
		if (fileType.contains("video")) {
			dest = new File(VIDEO_BASE_PATH + File.separator + fileName);
		} else if (fileType.contains("image")) {
			dest = new File(IMAGE_BASE_PATH + File.separator + fileName);
		}

		if (!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}
		file.transferTo(dest);
		return (fileType.contains("video") ? VIDEO_BASE_PATH : IMAGE_BASE_PATH) + File.separator + fileName;
	}

	public static String getVideoDuration(String filePath) {
		File file = new File(filePath);
		Encoder encoder = new Encoder();
		String duration = "00:00:00";
		try {
			MultimediaInfo m = encoder.getInfo(file);
			long seconds = m.getDuration() / 1000L;
			int hour = (int) (seconds / 3600L);
			int minute = (int) (seconds % 3600L / 60L);
			int second = (int) (seconds % 3600L % 60L);
			duration = (hour < 10 ? "0" + hour : new StringBuilder(String.valueOf(hour)).toString()) + ":"
					+ (minute < 10 ? "0" + minute : new StringBuilder(String.valueOf(minute)).toString()) + ":"
					+ (second < 10 ? "0" + second : new StringBuilder(String.valueOf(second)).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return duration;
	}
}
